﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthByDesign.Domain;
using System.Collections.Specialized;
using MyHealthByDesign.DataAccess.AccountDA;

namespace MyHealthByDesign.Views
{
    public partial class DynamicControls : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            AccountRepository ar = new AccountRepository();

            List<Question> lstQuestions = ar.GetQuestions(2);

            foreach (Question q in lstQuestions)
            {
                phPanels.Controls.Add(q.DisplayQuestion());
            }
            
            Session["Questions"] = lstQuestions;

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Go through fields and retrieve values
            // put in Dictionary Object

            List<Question> lstQs = (List<Question>) Session["Questions"];

            Dictionary<Question, NameValueCollection> formPost = new Dictionary<Question, NameValueCollection>();
            Dictionary<Question, NameValueCollection> returnValue = new Dictionary<Question, NameValueCollection>();

            MasterPage ctl00 = FindControl("ctl00") as MasterPage;
            ContentPlaceHolder MainContent = ctl00.FindControl("MainContent") as ContentPlaceHolder;

            foreach (Question q in lstQs)
            {
                returnValue = q.RetrieveQuestionValues(MainContent);
                foreach(var item in returnValue)
                {
                    formPost.Add(item.Key, item.Value);
                    
                }
            }

            Session["PostValues"] = formPost;
            Response.Redirect("DynamicResults.aspx");

        }

        
    }
}